﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyFriendAddControl : YuanlinUserControl
    {
        protected ValuedTextBox friendGreenNumberValuedTextBox;
        protected ValuedDropDownList categoryDropDownList;

        protected IButton SaveButton;
        protected IButton BackButton;
        private YuanlinUser loginUser = HttpContext.Current.User as YuanlinUser;

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bind();
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            CompanyFriend companyFriend = new CompanyFriend();

            CompanyGreenNumListRequest request = new CompanyGreenNumListRequest();
            request.GreenNum = this.friendGreenNumberValuedTextBox.Value;
            request.UserId = loginUser.EntityId.Value;
            EntityList companyGreenNums = BusinessManager.GetCompanyGreenNumList(RequestHelper.BuildGetCompanyGreenNumListRequest(request));
            if (companyGreenNums.Count == 0)
            {
                Globals.ShowMessage(this.Page, "CompanyFriendAdd", "当前绿商号不存在，请输入正确的绿商号！");
                return;
            }
            CompanyGreenNum companyGreenNum = companyGreenNums[0] as CompanyGreenNum;
            YuanlinUser user = UserManager.GetUser(companyGreenNum.UserId.Value) as YuanlinUser;
            companyFriend.FriendUserId.Value = user.EntityId.Value;

            companyFriend.FriendGreenNumber.Value = CensorWordManager.CensorWord(this.friendGreenNumberValuedTextBox.Value);
            companyFriend.CategoryId.Value = int.Parse(categoryDropDownList.Value);
            companyFriend.UserId.Value = loginUser.EntityId.Value;

            if (BusinessManager.CreateCompanyFriend(companyFriend) == -2)
            {
                Globals.ShowMessage(this.Page, "添加失败", "此信息已经存在！");
                return;
            }

            if (string.IsNullOrEmpty(loginUser.CompanyFriend))
            {
                loginUser.CompanyFriend = "1";
            }
            else
            {
                loginUser.CompanyFriend = (Int32.Parse(loginUser.CompanyFriend) + 1).ToString();
            }
            UserManager.UpdateUser(loginUser);

            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyFriendListUrl());
        }
        private void Bind()
        {
            CompanyFriendCategoryListRequest request = new CompanyFriendCategoryListRequest();
            request.UserId = loginUser.EntityId.Value;
            request.PageIndex = 0;
            request.PageSize = 1000;
            EntityList categories = BusinessManager.GetCompanyFriendCategoryList(RequestHelper.BuildGetCompanyFriendCategoryListRequest(request));

            categoryDropDownList.DataSource = categories;
            categoryDropDownList.DataValueField = "EntityId";
            categoryDropDownList.DataTextField = "Title";
            categoryDropDownList.DataBind();
        }
    }
}